package com.javasm.adminapi.module.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.adminapi.common.exception.ExceptionEnum;
import com.javasm.adminapi.common.exception.JavasmException;
import com.javasm.adminapi.login.service.LoginService;
import com.javasm.adminapi.module.activity.entity.AmActivity;
import com.javasm.adminapi.module.activity.service.AmActivityService;
import com.javasm.adminapi.module.activity.service.AmApiActivityService;


import com.javasm.adminapi.module.activity.vo.searchVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @className: AmApiActivityServiceImpl
 * @description:
 * @author: gfs
 * @date: 2025/10/17 19:57
 * @version: 0.1
 * @since: jdk17
 */
@Service("adminApiUserService")
public class AmApiActivityServiceImpl implements AmApiActivityService {

    @Resource
    AmActivityService amActivityService;

    @Resource
    LoginService loginService;



    @Override
    public PageInfo<AmActivity> listByPage(searchVo vo) {
        //开启分页功能，设置当前页为第 1 页，每页显示 10 条记录
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        //构造查询条件
        LambdaQueryWrapper<AmActivity> queryWrapper = new LambdaQueryWrapper<>();

        //name查询     通用的 name 来表示这一类查询条件
        String name = vo.getActivityName();
        if (name != null){
            queryWrapper.and(i->{
                i.like(AmActivity::getActivityName,name);
            });
        }
      //审批状态
        Integer rStatus = vo.getApproveStatus();
        if (rStatus != null){
            queryWrapper.and(i->{
                i.eq(AmActivity::getApproveStatus,rStatus);
            });
        }

        //活动状态
        Integer aStatus = vo.getType();
        if (aStatus != null){
            queryWrapper.and(i->{
                i.eq(AmActivity::getActivityType,aStatus);
            });
        }
        //活动开始时间
        String start = vo.getStart();
        String end = vo.getEnd();
        if (!StringUtils.isEmpty(start)&&!StringUtils.isEmpty(end)){
            queryWrapper.and(i->{
                //i.between(AdminUser::getRegTime,start,end);
                i.apply("date_format(activity_begin_time,'%Y-%m-%d') BETWEEN {0} AND {1}",start,end);
            });
        }

        //价格生效时间
        String pstart = vo.getPstart();
        String pend = vo.getPend();
        if (!StringUtils.isEmpty(pstart)&&!StringUtils.isEmpty(pend)){
            queryWrapper.and(i->{
                //i.between(AdminUser::getRegTime,start,end);
                i.apply("date_format(price_begin_time,'%Y-%m-%d') BETWEEN {0} AND {1}",pstart,pend);
            });
        }

        //指定查询结果按照id字段降序排序
        queryWrapper.orderByAsc(AmActivity::getId);

        //调用list方法，查询并获取结果列表
        List<AmActivity> list = amActivityService.list(queryWrapper);
        //将查询的数据列表封装成PageInfo对象，返回结果
        return new PageInfo<>(list);
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        //查询数据: 先获取要删除的活动记录，用于后续操作
        List<AmActivity> amActivityList = amActivityService.listByIds(List.of(ids));
        //批量清除缓存       AmActivity[0]返回一个与原列表大小相同的数组
        amActivityService.removeRedisActivity(amActivityList.toArray(new AmActivity[0]));

        //删除     List.of(ids)将数组转换为不可变列表
         amActivityService.removeByIds(List.of(ids));

    }

    @Override
    public void saveOrUpdate(AmActivity amActivity) {
        //先根据id判断是否为空，不为空，则说明已存在，进行更新操作，
        //if (!StringUtils.isEmpty(amActivity.getId())){
        if (amActivity.getId() != null && amActivity.getId() > 0){
            //判断活动名是否重复
            Long regNum = amActivityService.checkRegActivity(amActivity);
            //if (regNum > 0){
            //    throw new JavasmException(ExceptionEnum.Activity_Exist);
            //}
            amActivity.setActivityBeginTime(new Date());

        }
        //更新mybatisplus的操作
        amActivityService.saveOrUpdate(amActivity);
    }


}


